import Pools from "./pools";
import { hasOwn } from "./tools";

/**
 * 根据路由创建区域对象
 */
export default class Views {
  constructor(parent = undefined) {
    this.rootPopup = new Pools();
    this.popSign = "";
    this.routeViews = {};
    this.viewSign = "";
    this.$parent = parent;
    this.__item__ = null;
  }

  /**
   * 获取指定弹层对象
   * @param {String} popup 弹层标记名称
   * @returns
   */
  getLayer(popup) {
    let layer = popup ? "popup_" + popup : "rootPopup";
    if (hasOwn(this, layer) && this[layer] instanceof Pools) {
      return this[layer];
    } else {
      return null;
    }
  }

  /**
   * 获取焦点池对象，如果没有则创建新焦点池
   * @param {String} sign 标记名称
   * @returns Array
   */
  checkOrCreateLayer(popup) {
    let layer = popup ? "popup_" + popup : "rootPopup";
    if (hasOwn(this, layer)) {
      if (this[layer] instanceof Pools) {
        return this[layer];
      } else {
        // 可能由于手动修改导致
        throw `获取${popup}弹层失败`;
      }
    } else {
      return (this[layer] = new Pools());
    }
  }

  /**
   * 检测默认标记变量
   * @param {String} sign 分组标记名称
   * @param {String} popup 弹层标记名称
   * @returns Boolean
   */
  checkSignFlag(sign, popup) {
    let layer = this.getLayer(popup);
    let flag = layer ? layer.checkSign(sign) : false;
    return flag;
  }

  /**
   * 注册焦点
   * @param {Object} creater 焦点对象
   */
  registeLayer(creater) {
    let layer = this.checkOrCreateLayer(creater.popup);
    // 添加pools指向
    creater.__pools__ = layer;
    layer.registePool(creater);
  }

  /**
   * 注册路由节点对象
   * @param {*} BaseView
   */
  registeEl(BaseView) {
    // 获取父view对象
    this.__item__ = BaseView;
  }
}
